<?php

/**
 * 晒单白名单用户
 * @author tangyiwan
 */
ini_set('display_errors', 1);
error_reporting(-1);
ini_set('memory_limit', '512M');
require_once '/data/wwwroot/g_vendor/autoload.php';

use Biz\model\PhotoRelaGoods as mPhotoRelaGoods;
use Biz\model\trade\Order as mOrder;
use Biz\model\trade\OrderGoods as mOrderGoods;
use Doraemon\service\search\AdmUserSearch;
use Doraemon\model\AdmUserType;
use Doraemon\model\Member;
use Biz\cache\ShowOrderWhiteUser;

$filePath = "/home/adm_rsync_dir/order_photo_white_user.json";

$t1 = microtime(true);
$whiteUids = [];
$noWhiteUids = [];
$orderNOs = [];
$start = 0;
$page = 800;
do {
    $orderWhere = [
        'OR' => [
            'status' => mOrder::STATUS_SEND_OUT,
            'AND' => [
                'status' => mOrder::STATUS_SUCCESS,
                'confirm_time[>]' => date('Y-m-d', strtotime('-30 days'))
            ]
        ],
        'LIMIT' => [$start, $start + $page]
    ];
    $orderNOs = mOrder::select('order_no', $orderWhere);
    $userGoods = getOrderGoods($orderNOs);
    $uids = getUidsByFilter($userGoods);
    foreach ($userGoods as $uid => $goodsIds) {
        if (in_array($uid, $uids)) {
            $hsaPhoto = mPhotoRelaGoods::has(['uid' => $uid, 'goods_id' => $goodsIds, 'type' => 1]);
            if (!$hsaPhoto) {
                array_push($whiteUids, $uid);
            } else {
                array_push($noWhiteUids, $uid);
            }
            $whiteUids = array_diff($whiteUids, $noWhiteUids);
        }
    }
    $start = $start + $page;
} while ($orderNOs);
$whiteUids = array_diff($whiteUids, $noWhiteUids);
var_dump($whiteUids);
ShowOrderWhiteUser::setWhiteUser($whiteUids);

$t2 = microtime(true);
$time = $t2 - $t1;
$mTime = ($time * 1000) . 'ms';
var_dump("excute zdd all time:" . number_format($time, 2) . 's ' . $mTime);

function getOrderGoods($orderNos)
{
    $result = [];
    $orderGoodsList = mOrderGoods::select(['uid', 'goods_id'], ['order_no' => $orderNos, 'refund_order_no' => '', 'refund_amount' => 0]);
    var_dump('order goods Count:' . count($orderGoodsList));
    foreach ($orderGoodsList as $row) {
        $result[$row['uid']][] = $row['goods_id'];
    }
    return $result;
}

function getUidsByFilter($userGoods)
{
    $uids = array_keys($userGoods);
    $result = AdmUserSearch::getUidsByFilter([
        'query' => " id:(" . implode(' ', $uids) . " ) AND ( user_type:" . Member::AUTH_USER_DESIGNER . "  OR to_beconfirmd_identity:" . AdmUserType::AUTH_TAG_TO_DESIGNER . " ) "
    ], 1, count($uids));
    if ($result && $result['rows']) {
        $solorUid = $result['rows'];
        $uids = array_diff($uids, $solorUid);
    }
    var_dump('filter uids count:' . count($uids));
    return $uids;
}

